package com.lzy.huijinjiazu.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.gyf.barlibrary.ImmersionBar;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.lzy.huijinjiazu.R;
import com.lzy.huijinjiazu.di.component.DaggerFinanceComponent;
import com.lzy.huijinjiazu.di.module.FinanceModule;
import com.lzy.huijinjiazu.mvp.contract.FinanceContract;
import com.lzy.huijinjiazu.mvp.model.entity.Finance;
import com.lzy.huijinjiazu.mvp.presenter.FinancePresenter;
import com.lzy.huijinjiazu.mvp.ui.activity.SearchActivity;
import com.lzy.huijinjiazu.mvp.ui.adapter.FinanceAdapter;
import com.lzy.huijinjiazu.mvp.ui.adapter.FinanceTagAdapter;
import com.lzy.huijinjiazu.mvp.ui.dialog.FinanceTagDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.jess.arms.utils.Preconditions.checkNotNull;

//私行
public class FinanceFragment extends BaseFragment<FinancePresenter> implements FinanceContract.View {


    @BindView(R.id.fl_title)
    FrameLayout mFlTitle;
    @BindView(R.id.rv_tag)
    RecyclerView mRvTag;
    @BindView(R.id.rv_list)
    RecyclerView mRvList;
    @BindView(R.id.ll_top)
    LinearLayout mLlTop;
    private FinanceTagAdapter mFinanceTagAdapter;
    private FinanceAdapter mFinanceAdapter;
    private FinanceTagDialog tagDialog;
    private int type;//产品类型
    private int intelligent;//智能排序
    private int risk;//风险级别
    private int field;//投资领域

    public static FinanceFragment newInstance() {
        FinanceFragment fragment = new FinanceFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerFinanceComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .financeModule(new FinanceModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_finance, container, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {

        tagDialog = new FinanceTagDialog(getContext(), mLlTop);
        initRecyclerView();
        initTagData();
        initListData();
    }


    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.setTitleBar(getActivity(), mFlTitle);
    }

    private void initRecyclerView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRvTag.setLayoutManager(linearLayoutManager);
        mFinanceTagAdapter = new FinanceTagAdapter();
        mRvTag.setAdapter(mFinanceTagAdapter);

        ArmsUtils.configRecyclerView(mRvList, new LinearLayoutManager(getContext()));
        mFinanceAdapter = new FinanceAdapter();
        mRvList.setAdapter(mFinanceAdapter);
    }

    //顶部tag列表
    private void initTagData() {
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            data.add("");
        }
        mFinanceTagAdapter.setNewData(data);
    }

    private void initListData() {
        List<Finance> data = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            Finance finance = new Finance();
            finance.setComplete(i > 1);
            data.add(finance);
        }
        mFinanceAdapter.setNewData(data);
    }


    @OnClick({R.id.fl_type, R.id.fl_intelligent, R.id.fl_risk, R.id.fl_field, R.id.iv_search})
    public void onViewClicked(View view) {
        setClick(view);
    }

    private void setClick(View view) {
        switch (view.getId()) {
            case R.id.iv_search:
                ArmsUtils.startActivity(SearchActivity.class);
                break;
            case R.id.fl_type:
                List<String> data = new ArrayList<>();
                for (int i = 0; i < 3; i++) {
                    data.add("股权");
                }
                tagDialog.show();
                tagDialog.setData(data, type, 0xffe57f14);
                break;
        }
    }
}
